//**********************************分页查询 */
let data = {
    pagenum: 1,
    pagesize: 2,
}

// ***********1.0 查询
function queryList() {
    $.ajax({
        url: '/my/article/list',
        data: data,
        success: function(res) {
            let arr = []
            res.data.forEach(item => {
                arr.push(`
                <tr>
                <td>${item.title}</td>
                <td>${item.cate_name}</td>
                <td>${item.pub_date}</td>
                <td>${item.state}</td>
                <td>
                <a href='./edit.html?id=${item.id}' class="layui-btn layui-btn-xs">编辑</a>
                <button type="button" class="layui-btn layui-btn-xs layui-btn-danger" data-id="${item.id}" id="del_list">删除</button>
                </td>
            </tr>`)
            });
            $('tbody').html(arr.join(''))
            Tpage(res.total)
        }
    })
}

queryList()


// ************ 1.1 分页
let laypage = layui.laypage

function Tpage(total) {
    laypage.render({
        elem: 'page',
        count: total,
        limit: data.pagesize,
        limits: [2, 3, 5],
        curr: data.pagenum,
        layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
        jump: function(obj, first) {
            if (!first) {
                data.pagenum = obj.curr
                data.pagesize = obj.limit
                queryList()
            }
        }
    });

}


//**********************************下拉框 */
let form = layui.form
$.ajax({
    url: '/my/category/list',
    success: function(res) {
        let arr = []
        res.data.forEach(item => {
            arr.push(`<option value="${item.id}">${item.name}</option>`)
        })
        $('div #sel_category').append(res, arr.join(''))
        form.render()
    }
})

/***********************************下拉框删选 */
$('button:contains("删选")').on('click', function(e) {
    e.preventDefault()
    data.state = $('#sel_state').val()
    data.cate_id = $('#sel_category').val()
    if (data.state === '') {
        delete data.state
    }
    if (data.cate_id === '') {
        delete data.cate_id
    }
    queryList()
})

/***********************************删除文章 */
$('body').on('click', '#del_list', function() {
    let id = $(this).data('id')
    layer.confirm('您确定要删除吗?', function() {
        $.ajax({
            url: `/my/article/delete/${id}`,
            success: function(res) {
                layer.msg(res.message)
                data.pagenum = 1
                queryList()
            }
        })
    })
})